package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.AccountTypeCert;
import com.wdd.atcrowdfunding.bean.Cert;
import com.wdd.atcrowdfunding.manager.service.CertService;
import com.wdd.atcrowdfunding.manager.service.CertTypeService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.vo.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 分类管理
 */
@Controller
@RequestMapping("/certtype")
public class CurttypeController {

    @Autowired
    private CertService certService;
    @Autowired
    private CertTypeService certTypeService;

    @RequestMapping("/index")
    public String index(Model model){
        //1.查出横轴显示的数据
        List<String> types = Arrays.asList("商业公司","个体工商户","个人经营","政府及非营利组织");
        //2.查出纵轴显示的数据
        List<Cert> certs = certService.getALLCert();
        //3.横纵坐标的状态
        List<AccountTypeCert> accountTypeCerts = certTypeService.getAll();
            //二维矩阵
        Boolean[][] relations = new Boolean[certs.size()][types.size()];

        for (int i = 0; i < relations.length; i++) {
            for (int j = 0; j < relations[i].length; j++) {
                Cert cert = certs.get(i);
                String type = types.get(j);
                for (AccountTypeCert accountTypeCert :accountTypeCerts){
                    relations[i][j] = accountTypeCert.getAccttype().equals(type) && accountTypeCert.getCertid() == cert.getId();
                    if(relations[i][j]==true)
                        break;
                }
            }
        }

        model.addAttribute("types",types);
        model.addAttribute("certs",certs);
        model.addAttribute("relations",relations);
        return "certtype/type";
    }

    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult doAdd(Data data){
        AjaxResult ajaxResult = new AjaxResult();
        certTypeService.deleteAll();
        for(AccountTypeCert accountTypeCert :data.getAccountTypeCertList()){
            certTypeService.insert(accountTypeCert);
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }


}
